package cn.gx.kevin.common.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DynamicSqlHelper {

    static Pattern sqlVarReg = Pattern.compile("(\\$\\w+(\\s*[\\+\\-]\\s*\\d*[hdm])?)");

    static Pattern dateComputedReg =   Pattern.compile("(NOW|TODAY_START|TODAY_END|YESTEROAY_START|YESTEROAY_END|MONTH_START|MONTH_END|YEAR_START|YEAR_END|LAST_YEAR_START|LAST_YEAR_END|WEEK_START|WEEK_END|LAST_WEEK_START|LAST_WEEK_END)(\\s*[+-]\\s*\\d+)([YMDH])");

    /******
     * 提取sql中的$xxx 条件参数
     * *******/
    public static List<String> get$ArgsNameList(String sql){
        List<String> list = new ArrayList<>();
        Matcher matcher =   sqlVarReg.matcher(sql);
        while(matcher.find()){
            matcher.start() ;
            matcher.end();
            String findStr = matcher.group(0);
            list.add(findStr);
        }
        return list;
    }


    public  static Object getValueByComputed(String valueKey){
        Date date = getDateByFlag(valueKey);
        if(date != null){
            return date;
        }
        Object value = null;
        Matcher mat = dateComputedReg.matcher(valueKey);
        if(mat.find()){/******如果是时间计算*********/
            //String g0 = mat.group(0);
            String g1 = mat.group(1);
            String g2 = mat.group(2).replaceAll("\\s+","");
            String g3 = mat.group(3);
            Date now = getDateByFlag(g1);
            value = DateUtils.computedDateByNow(now,Integer.valueOf(g2),g3);
            //System.out.println(">>>>>>>");
        }
        return value;
    }

    public  static   Date getDateByFlag(String valueKey){
        if(valueKey.equalsIgnoreCase("NOW")){/****当前时间******/
            return new Date();
        }
        if(valueKey.equalsIgnoreCase("TODAY_START")){/****今天开始时间******/
            return DateUtils.getBeginDateOfNow();
        }
        if(valueKey.equalsIgnoreCase("TODAY_END")){/****今天结束时间******/
            return DateUtils.getEndDateOfNow();
        }
        if(valueKey.equalsIgnoreCase("YESTEROAY_START")){/****昨天开始时间******/
            return DateUtils.getBeginDateOfYesterday();
        }
        if(valueKey.equalsIgnoreCase("YESTEROAY_END")){/****昨天结束时间******/
            return DateUtils.getEndDateOfYesterDay();
        }
        if(valueKey.equalsIgnoreCase("MONTH_START")){/****本月开始时间******/
            return DateUtils.getBeginDateOfMonth();
        }
        if(valueKey.equalsIgnoreCase("MONTH_END")){/****本月结束时间******/
            return DateUtils.getEndDateOfMonth();
        }
        if(valueKey.equalsIgnoreCase("YEAR_START")){/****本年开始时间******/
            return DateUtils.getBeginDateOfYear();
        }
        if(valueKey.equalsIgnoreCase("YEAR_END")){/****本年结束时间******/
            return DateUtils.getEndDateOfYear();
        }
        if(valueKey.equalsIgnoreCase("LAST_YEAR_START")){/****去年开始时间******/
            return DateUtils.getBeginDateOfLastYear();
        }
        if(valueKey.equalsIgnoreCase("LAST_YEAR_END")){/****去年结束时间******/
            return DateUtils.getEndDateOfLastYear();
        }else if(valueKey.equalsIgnoreCase("WEEK_START")){
            return  DateUtils.getBeginDateOfWeek();
        }else if(valueKey.equalsIgnoreCase("WEEK_END")){
            return  DateUtils.getEndDateOfWeek();
        }else if(valueKey.equalsIgnoreCase("LAST_WEEK_START")){
            return  DateUtils.getBeginDateOfLastWeek();
        }else if(valueKey.equalsIgnoreCase("LAST_WEEK_START")){
            return  DateUtils.getEndDateOfLastWeek();
        }
        return null;
    }
}
